package com.hxzy.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.annotation.Log;
import com.hxzy.common.constant.UserConstants;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysDictType;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.utils.SecurityUtils;
import com.hxzy.system.domain.dto.DictTypeSearchDTO;
import com.hxzy.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "字典查询模型")
@RestController
@RequestMapping(value = "api")
public class DictTypeController  extends BaseController {
    @Autowired
    private ISysDictTypeService iSysDictTypeService;


    /**
     * 查询字典模型
     * @param dictDataDto
     * @return
     */
    @GetMapping(value = "/system/dict/data/list")
    @ApiOperation(value = "根据字典类型查询字典数据信息")
    public R selectAll (DictTypeSearchDTO dictDataDto) {
        IPage<SysDictType> page = this.iSysDictTypeService.search(dictDataDto);
        return super.pageVO(page);
    }

    /**
     * 新增字典模型
     * @param dict
     * @return
     */
    @PostMapping(value = "/system/dict/add")
    @ApiOperation(value = "新增字典")
    public R add (@RequestBody @Valid SysDictType dict ) {
        String s = this.iSysDictTypeService.checkDictTypeUnique(dict);
        if(s.equals(UserConstants.NOT_UNIQUE)){
            return R.build(AckCode.IS_EXISTS);
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        int l=this.iSysDictTypeService.insertDicType(dict);
        if(l!=1){
           return R.build(AckCode.IS_EXISTS);
        }
        return R.ok();
    }
    /**
     * 修改字典类型
     */

    @PutMapping(value = "/system/dict/update")
    @ApiOperation(value = "修改字典")
    public R edit(@Validated @RequestBody SysDictType dict )
    {
        SysDictType sysDictType = this.iSysDictTypeService.selectDictTypeById(dict.getDictId());
        if(sysDictType.getDictType().equals(dict.getDictType())){
            throw new Error("类型不能重复,必须是唯一的");
        }else {
            boolean b = this.iSysDictTypeService.updateBy(dict);
            if(b){
                return R.ok();
            }else{
                return   R.okHasData("修改失败");
            }
        }


    }

    /**
     * 查询字典模型
     * @param dictId
     * @return
     */
    @GetMapping(value = "/system/dict/type/{dictId}")
    @ApiOperation(value = "根据字典类型id查询字典数据信息")
    public R selectById (@PathVariable Long dictId) {
        SysDictType sysDictType = this.iSysDictTypeService.selectDictTypeById(dictId);
        return R.okHasData(sysDictType);
    }





    /**
     * 删除字典模型
     * @param dictId
     * @return
     */
    @DeleteMapping(value = "/system/dict/delete/{dictId}")
    @ApiOperation(value = "根据字典类型id删除字典数据信息")
    public R deletetById (@PathVariable Long[] dictId) {
       int i= this.iSysDictTypeService.deleteBy(dictId);
       if(i>0){
           return R.ok();
       }
        return R.build(AckCode.FILE_NOT_EXIST);
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/system/dict/type/optionselect")
    public R optionselect()
    {
        List<SysDictType> dictTypes = this.iSysDictTypeService.selectDictTypeAll();
        return R.okHasData(dictTypes);
    }


    /**
     * 清空缓存
     */
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/system/dict/type/refreshCache")
    public R clearCache()
    {
        this.iSysDictTypeService.clearCache();
        return R.ok();
    }

    /**
     * 导出
     * @param dictType
     * @return
     */
//    @GetMapping("/export")
//    public AjaxResult export(SysDictType dictType)
//    {
//        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
//        return util.exportExcel(list, "字典类型");
//    }

}



